﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Benchmark.Core.Communication.Model;
using Tcp.Performance.Toolkit.HubServer.Concurrent_Collection;

namespace HubServer
{


     class Program
     {
         readonly static ManualResetEvent Exclusion = new ManualResetEvent(false);
         static void Main(string[] args)
         {
             var hubCollection = new HubGuiCollection<HubGuiModel>() ;
             var ip = new List<string> { "localhost" };
             hubCollection.OnReadDelegate = new HubGuiCollection<HubGuiModel>.OnRead(RefreshCollection);

             var server = new Tcp.Performance.Toolkit.HubServer.HubServer(
                 ip,hubCollection
                 );

            
             Console.WriteLine("Starting Server...Connecting to client");
                 const string pipeName = "test123";
             int userinput = 0;
             while (userinput != 5)
             {
                 Console.WriteLine("1.Connect");
                 Console.WriteLine("2.Test");
                 Console.WriteLine("3.Initialize");
                 Console.WriteLine("4.Finish"); 
                 Console.WriteLine("5.Exit");
                 
                 userinput = Int32.Parse( Console.ReadLine().Split(' ').First());
                 switch (userinput)
                 {
                     case 1:

                         Console.WriteLine("Attempting to connect ..please check if client is started");

                         Task.Factory.StartNew(() => ip.ForEach(item =>
                         {
                             string pipeNameString = @"\\" + item + @"\pipe\" + pipeName;
                             
                             server.Connect(item.Trim(), pipeName);

                         }));
                         Exclusion.WaitOne();
                         break;

                     case 2:
                         Console.WriteLine("Attempting to test ..please wait");

                         // testing client from hub
                         Task.Factory.StartNew(() => ip.ForEach(item =>
                         {

                             server.Test(item);

                         }));
                         Exclusion.WaitOne();
             
                         break;

                     case 3:

                         Console.WriteLine("Attempting to Init ..please wait");    
                     // initializing with Hello World Model (Client Configured to read this)
                         Task.Factory.StartNew(() => ip.ForEach(item =>
                         {

                             server.Initialize<HelloWorldModel>(item, new HelloWorldModel() { Id = 1, Name = "Anup" });

                         }));
                         Exclusion.WaitOne();
                         break;
                     case 4:
                         Console.WriteLine("Attempting to Finish ..please wait");
                         Task.Factory.StartNew(() => ip.ForEach(item =>
                         {

                             server.Finish(item);

                         }));
                         Exclusion.WaitOne();
                         break;
                     case 5:
                         Console.WriteLine("Exiting");

                         break;

                     default:
                         Console.WriteLine( "Invalid choice..Please reenter");
                         break;
                 }
                 
             }

             Console.WriteLine("Press any key to exit....");
             Console.ReadKey();
         }




         public static  void RefreshCollection(HubGuiModel model)
         {

             //replace with LINQ query
             Console.WriteLine( "###########################################");
             Console.WriteLine( "Recieved status from Concurrent Collection");
             if (model.IsConnect)
                 Console.WriteLine("Server Connected  ");
             if (model.IsTest)
             Console.WriteLine("Testing Successfull" );
             if (model.IsInit)
                 Console.WriteLine("Initialization  Successfull");
             if (model.IsFinish)
                 Console.WriteLine("Finsih Successfull ");
             Console.WriteLine("###########################################");

             Console.WriteLine( );
             Exclusion.Set();



         }

    }
}
